package com.bjpowernode.rbac.service.impl;

import com.bjpowernode.rbac.entity.Department;
import com.bjpowernode.rbac.mapper.DepartmentMapper;
import com.bjpowernode.rbac.query.QueryObject;
import com.bjpowernode.rbac.service.DepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jy
 * @version 1.0
 * @date 2022/2/7 19:12
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public PageInfo<Department> selectDepartmentByCondition(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<Department> list = departmentMapper.selectByCondition(queryObject);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Department department) {
        Department department1 = departmentMapper.selectByPrimaryKey(department.getId());
        if (department.getId() != null || department1 != null){
            departmentMapper.updateByPrimaryKey(department);
        }else {
            departmentMapper.insert(department);
        }
    }

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }
}
